package com.zycloud.service.impl;

import com.zycloud.facade.dto.UserDto;
import com.zycloud.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private List<UserDto> userDtos = new ArrayList<>();


    @Override
    public UserDto loadUserByUserId(int id) {
        return getUser(id);
    }

    private UserDto getUser(int id) {
        if (userDtos == null || userDtos.size() == 0) {
            for (int i = 0; i < 10; i++) {
                UserDto userDto = new UserDto();
                userDto.setId(i);
                userDto.setAge(i + 10);
                userDto.setName(RandomStringUtils.randomAlphabetic(4));
                userDtos.add(userDto);
            }
        }

        for (UserDto userDto : userDtos) {
            if (userDto.getId() == id) {
                return userDto;
            }
        }
        return null;
    }

}
